package com.aimusic.controller;


import com.aimusic.domain.Result;
import com.aimusic.domain.po.FriendRequests;
import com.aimusic.domain.po.Messages;
import com.aimusic.domain.vo.FriendVO;
import com.aimusic.service.IFriendRequestsService;
import com.aimusic.service.IFriendshipsService;
import com.aimusic.service.IMessagesService;
import com.aimusic.service.IUserProfileService;
import com.aimusic.utils.CurrentHolder;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 好友关系表（记录用户与好友的关联） 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-09-29
 */
@RestController
@RequestMapping("/api/chat")
@RequiredArgsConstructor
@Slf4j
public class ChatController {

    private final IFriendshipsService friendshipsService;
    private final IFriendRequestsService friendRequestsService;
    private final IUserProfileService userProfileService;
    private final IMessagesService messageService;
    /**
     * 获取好友列表
     * @return
     */

    @GetMapping("/friends")
    public Result getFriends() {
        //  获取好友列表
        List<FriendVO> friends = friendshipsService.getFriends();
        return Result.success(friends);
    }

    /**
     * 获取好友请求列表
     * @return
     */
    @GetMapping("/friend-requests")
    public Result getFriendRequests() {
        //  获取好友请求列表
        List<FriendVO> friendRequests = friendRequestsService.getFriendRequests();
        return Result.success(friendRequests);
    }

    /**
     * 接受好友请求
     * @return
     */
    @PostMapping("/friend-requests/accept")
    public Result acceptFriendRequest(@RequestBody Long id) {
        //同意添加好友
        friendRequestsService.acceptFriendRequest(id);
        return Result.success("true");
    }

    /**
     * 拒绝好友请求
     * @return
     */
    @PostMapping("/friend-requests/reject")
    public Result rejectFriendRequest(@RequestBody Long id) {
        friendRequestsService.removeById(id);
        return Result.success("true");
    }

    /**
     * 根据手机号搜索用户
     * @return
     */
    @GetMapping("/search-by-phone")
    public Result searchByPhone(@RequestParam("phone") String phone) {
        //  根据手机号搜索用户
        FriendVO friendVO = friendRequestsService.getByPhone(phone);

        return Result.success(friendVO);
    }

    @PostMapping("/friend-requests/send")
    public Result sendFriendRequest(@RequestBody FriendRequests friendRequests) {
        friendRequests.setFromUserId(CurrentHolder.getCurrentId().longValue());
        friendRequestsService.save(friendRequests);
        return Result.success();
    }

    /**
     * 获取聊天记录
     * @param currentUserId
     * @param friendId
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/history")
    public Result getChatHistory(
            @RequestParam Long currentUserId,
            @RequestParam Long friendId,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer size) {
        return Result.success(messageService.getChatHistory(currentUserId, friendId, page, size));
    }
}
